package com.codexsoft.test;

import com.codexsoft.test.entities.User;
import com.codexsoft.test.jpamodel.EMF;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 26.08.11
 * Time: 10:16
 * To change this template use File | Settings | File Templates.
 */
public class SalutUserPanel extends Panel {

    public SalutUserPanel(String id) {
        super(id);
        EntityManager em = EMF.get().createEntityManager();
        String query = "SELECT FROM " + User.class.getName();
        List<User> userList = em.createQuery(query).getResultList();
        User loggeduser = LastLoggedUser(userList);
        if (IsUserLogged(userList) && (loggeduser != null)) {
            add(new Label("salutation",
                    "Hello, " + loggeduser.getName()));
            Form form = new Form("exitform");
            form.add(new Link("exituseraccount") {
                @Override
                public void onClick() {
                    EntityManager em = EMF.get().createEntityManager();
                    String query = "SELECT FROM " + User.class.getName();
                    List<User> userList = em.createQuery(query).getResultList();
                    LastLoggedUser(userList).setLogged(false);
                    try {
                        em.merge(LastLoggedUser(userList));
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        em.close();
                    }
                    setResponsePage(new UInfoPage());
                }
            });
            add(form);
        } else {
            add(new Label("salutation", "some mistake accured"));
            add(new WizardLink("exituseraccount", RegistrationWizard.class));
        }
    }

    public User UserLogged(String nick, String password, List<User> userList) {
        User result = null;
        for (User u : userList)
            if ((u.getNickname().equals(nick)) && (u.getPassword().equals(password))) {
                result = u;
            }
        return result;
    }

    public boolean IsUserLogged(List<User> userList) {
        boolean result = false;
        for (User u : userList)
            if (u.isLogged())
                result = true;
        return result;
    }

    public User LastLoggedUser(List<User> userList) {
        User result = null;
        for (User u : userList)
            if (u.isLogged())
                result = u;
        return result;
    }
}
